<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-8-9
 * Time: 上午 10:40
 */
/*数据库初始化*/
include 'common.php';
$configPath = getAbsolutePath(__DIR__,'../../config/config.php');
$config = require $configPath;
function getMysql()
{
    global $config;
    $_mysqli = new mysqli($config['db']['DB_HOST'], $config['db']['DB_USER'], $config['db']['DB_PASS']);
    if (mysqli_connect_errno()) {
        exit('连接数据库出错');
    }
    //如果数据库不存在就先建立数据库
    $_mysqli->query("CREATE DATABASE IF NOT EXISTS " . $config['db']['DB_NAME'] . " DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;");
    $_mysqli->query("USE " . $config['db']['DB_NAME']);
    $_mysqli->close();
    $link = mysqli_connect($config['db']['DB_HOST'], $config['db']['DB_USER'], $config['db']['DB_PASS'], $config['db']['DB_NAME'], $config['db']['DB_PORT']);
    mysqli_query($link, 'set name ' . $config['db']['charset']);
    return $link;
}

$link = getMysql();
/*建立表信息*/
function createTable()
{
    global $config, $link;
    $resultData = array('成功创建' => 0, '创建失败' => 0, '异常抛出' => '');
    $tableConfig = $config['install']['table'];
    //构造sql
    foreach ($tableConfig as $tableName => $table) {
        $sql = 'CREATE TABLE IF NOT EXISTS ';
        $sql = $sql . $tableName . ' (';
        foreach ($table as $key => $value) {
            if ($key != 'primary_key') {
                $sql = $sql . $key . ' ' . $value . ',';
            } else {
                $sql = $sql . $value;
            }
        }
        $sql = $sql . ') ENGINE = MyISAM CHARACTER SET = ' . $config['db']['charset'] . ' COLLATE = ' . $config['db']['sortRule'] . ' ROW_FORMAT = Dynamic;';
        //创建
        try {
            $result = mysqli_query($link, $sql);
            if ($result) {
                $resultData['成功创建'] += 1;
            } else {
                $resultData['创建失败'] += 1;
            }
        } catch (Exception  $e) {
            $err = '异常：' . $e->getMessage();
            $resultData['异常抛出'] += 'SQL:' . $sql . '\\n 异常：' . $err;
            echo $err;
        }
    }
    /*return $resultData;*/
    header('Refresh:0;url=/view/test.html');
}

/*安装sql文件*/
function install_SQLfile()
{
    global $config, $link;
    $sqlBasePath = getAbsolutePath($config['install']['SQL_path']['configBasePath'], $config['install']['SQL_path']['path']);
    //获取数据库安装路径
    $result = mysqli_query($link, 'select @@basedir as basePath from dual');
    $mysqlBasePath = mysqli_fetch_assoc($result)['basePath'] . '/bin';
    $cmd = "cd /d " . $mysqlBasePath . " && mysql -h" . $config['db']['DB_HOST'] . " -u" . $config['db']['DB_USER'] . " -p" . $config['db']['DB_PASS'] . " " . $config['db']['DB_NAME'] . "< " . $sqlBasePath;
    try {
        exec($cmd, $res);
        header('Refresh:0;url=/view/test.html');
    } catch (Exception $e) {
        header('Refresh:0;url=/view/install.html');
    }
}
/*请求执行*/
if($_GET['method']=='configInstall'){
    createTable();
}elseif($_GET['method']=='installSQLfile'){
    install_SQLfile();
}

